package com.jlcindia.struts2;

import java.util.*;

import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;
import javax.servlet.http.*;

import org.apache.struts2.interceptor.*;

import com.opensymphony.xwork2.*;

public class TestAction extends ActionSupport implements Preparable,
		ModelDriven<User>, ServletRequestAware, ServletResponseAware,
		ServletRequestListener, RequestAware, SessionAware, ApplicationAware {
	
	private List<Student> students;
	
	public TestAction(){
		System.out.println("Test Action Cons.....");
		students=new ArrayList<Student>();
		List<String> ph=new ArrayList<String>();
		ph.add("11111");
		ph.add("22222");
		ph.add("33333");
		ph.add("44444");
		Address add=new Address("blore",ph);
		List<Address> adds=new ArrayList<Address>();
		adds.add(add);
		adds.add(add);
		adds.add(add);
		Student stu=new Student("srinivas",adds);
		students.add(stu);
		students.add(stu);
		students.add(stu);
		students.add(stu);
	}
	private User user;
	private HttpServletRequest request;
	private HttpServletResponse response;
	private Map requestMap=null;
	private Map sessionMap=null;
	private Map contextMap=null;
	private Hello hello=new Hello();
	
	
	public List<Student> getStudents() {
		return students;
	}

	public void setStudents(List<Student> students) {
		this.students = students;
	}

	public Hello getHello() {
		return hello;
	}

	public void setHello(Hello hello) {
		this.hello = hello;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public String execute(){
		System.out.println("execute method()");
		System.out.println(user.getName());
		System.out.println(user.getEmail());
		System.out.println(user.getPhone());
		setData();
		showData();
		return "success";
	}
	
	public void validate(){
		System.out.println("validate() method");
		if(user.getName()==null||user.getName().length()==0){
			addActionError("Name is Required");
			addFieldError("name", "Name is Required");
		}
		
		if(user.getEmail()==null||user.getEmail().length()==0){
			addActionError("Email is Required");
			addFieldError("email1", "Email is Required");
		}
		
		if(user.getPhone()==null||user.getPhone().length()==0){
			addActionError("Phone is Required");
			addFieldError("phone1", "Phone is Required");
		}
	}
	
	private void showData() {
		System.out.println(request.getAttribute("EMAIL"));
		System.out.println(request.getRemoteAddr());
		System.out.println(response.getLocale().getLanguage());
		System.out.println(getLocale().getLanguage());
		System.out.println(requestMap.get("PHONE"));
		User u=(User)sessionMap.get("USER");
		System.out.println(u.getEmail());
		System.out.println(u.getPhone());
		System.out.println(contextMap.get("NAME"));
		
	}

	private void setData() {
		request.setAttribute("EMAIL", user.getEmail());
		request.setAttribute("USER", user);
		requestMap.put("PHONE", user.getPhone());
		requestMap.put("MSG", "WELCOME to JLC");
		sessionMap.put("USER", user);
		contextMap.put("NAME", user.getName());
		ArrayList<String> names=new ArrayList<String>();
		names.add("Aniket");
		names.add("aman");
		names.add("bp. singh");
		names.add("suraj");
		sessionMap.put("NAMES", names);
		ArrayList<User> users=new ArrayList<User>();
		users.add(user);
		users.add(user);
		users.add(user);
		users.add(user);
		users.add(user);
		contextMap.put("USERS", users);
		sessionMap.put("JLC", students);
		
	}

	public void setApplication(Map contextMap) {
		
		this.contextMap=contextMap;

	}

	
	public void setSession(Map sessionMap) {
		
		this.sessionMap=sessionMap;

	}

	
	public void setRequest(Map requestMap) {

		this.requestMap=requestMap;

	}


	public void requestDestroyed(ServletRequestEvent arg0) {
		

	}

	
	public void requestInitialized(ServletRequestEvent arg0) {
		

	}

	
	public void setServletResponse(HttpServletResponse response) {

		this.response=response;

	}

	
	public void setServletRequest(HttpServletRequest request) {

		this.request=request;

	}

	
	public void prepare() throws Exception {
user=new User();
		

	}

	
	public User getModel() {

		
		return user;
	}

	
}
